package me.firstapp.spi.zerone.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.zerone.JsonCqsscLotteryResult;
import me.firstapp.common.module.zerone.JsonCqsscPlan;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.zerone.CqsscService;

@Service
public class CqsscServiceImpl extends BaseServiceImpl implements CqsscService {

	@Override
	public PageObject<JsonCqsscLotteryResult> findCqsscLotteryResultPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonCqsscLotteryResult> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/zerone/cqsssc/findCqsscLotteryResultPage.do", "分页获取开奖结果",
					searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonCqsscLotteryResult> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonCqsscLotteryResult> resultList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonCqsscLotteryResult cqsscLotteryResult = JSONObject.toJavaObject(_jsonObject,
										JsonCqsscLotteryResult.class);
								resultList.add(cqsscLotteryResult);
							}
						}
					}
					jsonPage.setList(resultList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonCqsscLotteryResult> addCqsscLotteryResult(String issueNo, String prizeNo) {
		SingleObject<JsonCqsscLotteryResult> result = new SingleObject<JsonCqsscLotteryResult>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("issueNo", issueNo);
			parameters.put("prizeNo", prizeNo);

			JSONObject resultJson = api(parameters, "/zerone/cqsssc/addCqsscLotteryResult.do", HttpMethod.POST,
					"新增开奖结果");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonCqsscLotteryResult cqsscLotteryResult = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonCqsscLotteryResult.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(cqsscLotteryResult);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonCqsscLotteryResult> updateCqsscLotteryResult(Long id, String issueNo, String prizeNo) {
		SingleObject<JsonCqsscLotteryResult> result = new SingleObject<JsonCqsscLotteryResult>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("issueNo", issueNo);
			parameters.put("prizeNo", prizeNo);

			JSONObject resultJson = api(parameters, "/zerone/cqsssc/updateCqsscLotteryResult.do", HttpMethod.POST,
					"修改开奖结果");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonCqsscLotteryResult cqsscLotteryResult = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonCqsscLotteryResult.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(cqsscLotteryResult);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> addPlans(String planIssueNo, int notWinNum) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("planIssueNo", planIssueNo);
			parameters.put("notWinNum", notWinNum);

			JSONObject resultJson = api(parameters, "/zerone/cqsssc/addPlans.do", HttpMethod.POST, "新增计划");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonCqsscPlan> findCqsscPlanPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonCqsscPlan> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/zerone/cqsssc/findCqsscPlanPage.do", "分页获取计划", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonCqsscPlan> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonCqsscPlan> resultList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonCqsscPlan jsonCqsscPlan = JSONObject.toJavaObject(_jsonObject, JsonCqsscPlan.class);
								resultList.add(jsonCqsscPlan);
							}
						}
					}
					jsonPage.setList(resultList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonCqsscLotteryResult> findCqsscLotteryResultById(Long id) {
		SingleObject<JsonCqsscLotteryResult> result = new SingleObject<JsonCqsscLotteryResult>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);

			JSONObject resultJson = api(parameters, "/zerone/cqsssc/findCqsscLotteryResultById.do", HttpMethod.POST,
					"查询结果");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonCqsscLotteryResult cqsscLotteryResult = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonCqsscLotteryResult.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(cqsscLotteryResult);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
